'use strict';

const Controller = require('egg').Controller;

class DoLoginController extends Controller {
    async index() {
        const { ctx } = this;
        // console.log(ctx.request.body);
        var { username, pwd } = ctx.request.body;
        var db = this.app.mongodb;
        const collection = await db.collection("user");
        var res = await collection.find({ username,pwd }).toArray();
        var nickname = await collection.find({ username }).toArray();
        // console.log(res);
        if(res.length){
            ctx.body = "<script>alert('登录成功');location.href='/form'</script>"
        }else{
            ctx.body = "<script>alert('用户名或密码错误');location.href='/login'</script>";
            if(!nickname.length){
                ctx.body = "<script>alert('用户名未注册，请先注册');location.href='/register'</script>"
            }
        }
        
    }
}

module.exports = DoLoginController;
